/**
 * 
 */
package systole.persistence.brokersDB;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;

import systole.domain.clinicalInformation.Sport;
import systole.domain.clinicalInformation.SportPatient;
import systole.domain.persons.Patient;
import systole.exceptions.ExceptionDAO;
import systole.persistence.FacadeDB;
import systole.persistence.brokersInterface.SportByPatientBroker;
import systole.view.messages.ErrorMessages;

/**
 * @author jmj
 * 
 */
public class SportByPatientBrokerDB extends BrokerDB implements
        SportByPatientBroker {

    /**
     * 
     */
    public SportByPatientBrokerDB() {
        super();
    }

    /*
     * (non-Javadoc)
     *
     * @see
     * systole.persistence.brokersInterface.SportByPatientBroker#existSportByPatient
     * (systole.domain.clinicalInformation.Sport,
     * systole.domain.persons.Patient)
     */
    @Override
    public boolean existSportByPatient(Sport sport, Patient patient)
            throws ExceptionDAO {
        try {
            this.logger.logDebug("exist sport by patient");
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            @SuppressWarnings("unchecked")
            List<SportPatient> list = currentSession.createCriteria(
                    SportPatient.class).add(
                    Restrictions.and(Restrictions.eq("patient", patient),
                    Restrictions.eq("sport", sport))).list();
            this.logger.logDebug("exist sport by patient successfully");
            return !list.isEmpty();
        } catch (HibernateException e) {
            this.logger.logError("error on exist sport by patient, msg: "
                    + e.getMessage());
            throw new ExceptionDAO(
                    "No se verificar si ya existe la actividad física del paciente",
                    e.fillInStackTrace());
        }
    }

    /*
     * (non-Javadoc)
     *
     * @see
     * systole.persistence.brokersInterface.SportByPatientBroker#getAllPatientSports
     * ()
     */
    @Override
    public List<SportPatient> getAllPatientSports(Patient patient)
            throws ExceptionDAO {
        try {
            this.logger.logDebug("Getting all patient sports");
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            @SuppressWarnings("unchecked")
            List<SportPatient> list = currentSession.createCriteria(
                    SportPatient.class).add(Restrictions.eq("patient", patient)).list();
            this.logger.logDebug("get patient sports");
            return list;
        } catch (HibernateException e) {
            this.logger.logError("error on get all patient sports, msg: "
                    + e.getMessage());
            throw new ExceptionDAO(
                    "No se pudieron obtener las actividades físicas del paciente",
                    e.fillInStackTrace());
        }
    }

    /*
     * (non-Javadoc)
     *
     * @see
     * systole.persistence.brokersInterface.SportByPatientBroker#insert(systole
     * .domain.clinicalInformation.SportPatient)
     */
    @Override
    public void insert(SportPatient sportPatient) throws ExceptionDAO {
        try {
            this.logger.logDebug("savig patient sport");
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            currentSession.save(sportPatient);
            this.logger.logDebug("saved patient sport");
        } catch (HibernateException e) {
            this.logger.logError("error on save patient sport, msg: "
                    + e.getMessage());
            throw new ExceptionDAO(ErrorMessages.CHANGES_NOT_SAVE, e.fillInStackTrace());
        }
    }

    /*
     * (non-Javadoc)
     *
     * @see
     * systole.persistence.brokersInterface.SportByPatientBroker#update(systole
     * .domain.clinicalInformation.SportPatient)
     */
    @Override
    public void update(SportPatient sportPatient) throws ExceptionDAO {
        try {
            this.logger.logDebug("updating patient sport");
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            currentSession.update(sportPatient);
            this.logger.logDebug("updated patient sport");
        } catch (HibernateException e) {
            this.logger.logError("error on update patient sport, msg: "
                    + e.getMessage());
            throw new ExceptionDAO(ErrorMessages.CHANGES_NOT_SAVE, e.fillInStackTrace());
        }
    }

    public void delete(SportPatient sportPatient) throws ExceptionDAO {
        try {
            this.logger.logDebug("deleting patient sport");
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            currentSession.delete(sportPatient);
            this.logger.logDebug("deleted patient sport");
        } catch (HibernateException e) {
            this.logger.logError("error on delete patient sport, msg: "
                    + e.getMessage());
            throw new ExceptionDAO("No se pudo eliminar", e.fillInStackTrace());
        }
    }
}
